﻿//-----------------------------------------------------------------------
// <copyright file="BookFindBookDetailPage.xaml.cs" company="FIT VUT">
//     Copyright (c) FIT VUT. All rights are reserved.
// </copyright>
//----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BookCaching.Data;
using BookCaching.Settings;
using BookCaching.Localization;
using Microsoft.Phone.Shell;

namespace BookCaching.Pages
{
    /// <summary>
    /// Page displays information about a book acquired after finding a book, to confirm the finding.
    /// </summary>
    public partial class BookFindBookDetailPage : PhoneApplicationPage
    {
        private Book book;

        public BookFindBookDetailPage()
        {
            this.InitializeComponent();
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (NavigationContext.QueryString.ContainsKey("BookID"))
            {

                Knihotoc_WCFService.Knihotoc_WP7ServiceClient client = new Knihotoc_WCFService.Knihotoc_WP7ServiceClient(); 

                try
                {
                    client.GetBookByIDCompleted += client_GetBookByIDCompleted;
                    client.GetBookByIDAsync(Convert.ToInt32(NavigationContext.QueryString["BookID"]));
                }
                catch
                {
                    return;
                }
            }
        }

        private void client_GetBookByIDCompleted(object sender, Knihotoc_WCFService.GetBookByIDCompletedEventArgs e)
        {
            if (e.Error != null || e.Result == null)
            {
                return;
            }

            Knihotoc_WCFService.Book findBook = e.Result; 
            book = new Book(findBook);
            bookTitleText.Text = book.Title;
            authorText.Text = book.Author.FullName;
            iDText.Text = book.ID.ToString();
            
        }

        private void client_BorrowBookByIDCompleted(object sender, Knihotoc_WCFService.BorrowBookByIDCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                return;
            }
            if (e.Result != null)
            {
                MessageBox.Show(e.Result);
            }
            else
            {
                //ShellToast toast = new ShellToast();
                //toast.Title = "BookCaching";
                //toast.Content = "toast";//LocalizedStrings.Get("BookBorrow");
                //toast.Show();
                MessageBox.Show(LocalizedStrings.Get("BookBorrow"));
            }
            NavigationService.Navigate(new Uri("/Pages/MainPage.xaml", UriKind.Relative));
        }

        private void TakeBookButton_Click(object sender, RoutedEventArgs e)
        {
            if (iDText.Text != "")
            {
                Knihotoc_WCFService.Knihotoc_WP7ServiceClient client = new Knihotoc_WCFService.Knihotoc_WP7ServiceClient();

                try
                {
                    client.BorrowBookByIDCompleted += client_BorrowBookByIDCompleted;
                    client.BorrowBookByIDAsync(Convert.ToInt32(iDText.Text), CurrentSettings.Instance.User.ID);
                }
                catch
                {
                    return;
                }
            }
        }

        private void OkButton_Click(object sender, RoutedEventArgs e)
        {
            Knihotoc_WCFService.Knihotoc_WP7ServiceClient client = new Knihotoc_WCFService.Knihotoc_WP7ServiceClient();

            try
            {
                client.GetBookByIDCompleted += client_GetBookByIDCompleted;
                client.GetBookByIDAsync(Convert.ToInt32(iDText.Text));
            }
            catch
            {
                return;
            }
        }

        private void iDText_GotFocus(object sender, RoutedEventArgs e)
        {
            this.iDText.Foreground = CurrentSettings.Instance.ForegroundColor;
        }

        private void iDText_LostFocus(object sender, RoutedEventArgs e)
        {
            this.iDText.Foreground = CurrentSettings.Instance.BackgroundColor;
        }
    }
}